package com.metaverse.backend.service;

import com.metaverse.backend.domain.RoomLog;
import com.metaverse.backend.domain.User;
import com.metaverse.backend.dto.PageQuery;
import com.metaverse.backend.repo.RoomLogRepo;
import com.metaverse.backend.utils.JpaUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

@AllArgsConstructor
@Data
@Slf4j
@Service
public class RoomLogService {
    private RoomLogRepo roomLogRepo;

    /**
     * 分页数据
     * @param pageQuery
     * @return
     */
    public Page<RoomLog> all(PageQuery pageQuery) {
        return roomLogRepo.findAll(JpaUtils.toSpecification(pageQuery, RoomLog.class),
                JpaUtils.toPageRequest(pageQuery));
    }


    /**
     * 添加对局记录
     * @param roomId
     * @param round_id
     * @param gameVersion
     * @param user
     */
    public void add(String roomId,String round_id, String gameVersion, User user){
        RoomLog roomLog = new RoomLog();
        roomLog.setRoomId(roomId);
        roomLog.setOpenid(user.getOpenid());
        roomLog.setUserId(user.getId());
        roomLog.setNickname(user.getNickname());
        roomLog.setAvatar(user.getAvatar());
        roomLog.setRoundId(round_id);
        roomLog.setGameVersion(gameVersion);
        roomLogRepo.save(roomLog);
    }

    public RoomLog findByRoomId(String openid,String roomId,String roundId){
       return roomLogRepo.findByRoomId(openid,roomId,roundId);
    }

    public RoomLog findLastByRoundId(String roundId){
        return roomLogRepo.findLastByRoundId(roundId);
    }

    public RoomLog getLastRoomLog(){
        return roomLogRepo.findLastRoom();
    }
}
